package com.laiyima.zhongjiang.linghuilv.demo.Serviceprovider;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.laiyima.zhongjiang.linghuilv.demo.R;

import org.json.JSONObject;
import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.math.BigDecimal;
import java.text.DecimalFormat;

import me.imid.swipebacklayout.lib.app.SwipeBackActivity;

public class MyStatisticsActivity extends SwipeBackActivity implements View.OnClickListener {
    private String TAG="MyStatisticsActivity";
    private int msCode;
    private LinearLayout imageView;
    private LinearLayout incomeLinearLayout,userLinearLayout;

    private TextView today,yesterday,month,year,total;
    private TextView uesr_today,user_yesterday,user_month,useer_year,user_total;
    protected boolean useThemestatusBarColor = false;//是否使用特殊的标题栏背景颜色，android5.0以上可以设置状态栏背景色，如果不使用则使用透明色值
    protected void setStatusBar() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {//5.0及以上
            View decorView = getWindow().getDecorView();
            int option = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                    | View.SYSTEM_UI_FLAG_LAYOUT_STABLE;
            decorView.setSystemUiVisibility(option);
            //根据上面设置是否对状态栏单独设置颜色
            if (useThemestatusBarColor) {
                // getWindow().setStatusBarColor(getResources().getColor(R.color.colorTheme));
            } else {
                getWindow().setStatusBarColor(Color.TRANSPARENT);
            }
        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {//4.4到5.0
            WindowManager.LayoutParams localLayoutParams = getWindow().getAttributes();
            localLayoutParams.flags = (WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS | localLayoutParams.flags);
        }
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M ) {//android6.0以后可以对状态栏文字颜色和图标进行修改
            getWindow().getDecorView().setSystemUiVisibility( View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN|View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
        }
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.my_statistics);
        imageView= (LinearLayout) findViewById(R.id.my_statics_back);
        incomeLinearLayout= (LinearLayout) findViewById(R.id.to_my_income);
        userLinearLayout= (LinearLayout) findViewById(R.id.to_my_user);


        imageView.setOnClickListener(this);
        incomeLinearLayout.setOnClickListener(this);
        userLinearLayout.setOnClickListener(this);


        today= (TextView) findViewById(R.id.static_day);
        yesterday= (TextView) findViewById(R.id.static_yesterday);
        month= (TextView) findViewById(R.id.static_month);
        year= (TextView) findViewById(R.id.static_year);
        total= (TextView) findViewById(R.id.static_total);

        uesr_today= (TextView) findViewById(R.id.add_today);
        user_yesterday= (TextView) findViewById(R.id.user_yesterday);
        user_month= (TextView) findViewById(R.id.add_month);
        useer_year= (TextView) findViewById(R.id.useradd_year);
        user_total= (TextView) findViewById(R.id.user_total);
        setStatusBar();
        intView();
        intView2();
        setAndroidNativeLightStatusBar(this,false);
    }
    private static void setAndroidNativeLightStatusBar(Activity activity, boolean dark) {
        View decor = activity.getWindow().getDecorView();
        if (dark) {
            decor.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
        } else {
            decor.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
        }
    }


    private void intView() {
        SharedPreferences sharedPreferences = getSharedPreferences("userinfo.txt", Context.MODE_PRIVATE);
        String userID = sharedPreferences.getString("userID", "");
        RequestParams params = new RequestParams("http://zljl.laiima.com/index.php?s=/lfl_api/Commonlfl/earnings");
        //需要带入的参数
        params.addBodyParameter("uid", userID);
        x.http().post(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                try {
                    JSONObject jsonObject = new JSONObject(result);
                    String data = jsonObject.optString("list", null);
                    if (!TextUtils.isEmpty(data)){
                        JSONObject jsonObjec = new JSONObject(data);
                       String  mytoday= jsonObjec.optString("today", "");
                        Double z1 = Double.parseDouble(mytoday);
                        DecimalFormat decimalFormat1 = new DecimalFormat("0.00");
                        String x1 = decimalFormat1.format(z1);
                        BigDecimal bg1=new BigDecimal(x1);
                        today.setText(bg1+"");
                       String  myyesterday= jsonObjec.optString("yesterday", "");
                        Double z2 = Double.parseDouble(myyesterday);
                        String x2 = decimalFormat1.format(z2);
                        BigDecimal bg2=new BigDecimal(x2);
                        yesterday.setText(bg2+"");
                       String  mymonth= jsonObjec.optString("month", "");
                        Double z3 = Double.parseDouble(mymonth);
                        String x3 = decimalFormat1.format(z3);
                        BigDecimal bg3=new BigDecimal(x3);
                       month.setText(bg3+"");
                       String  myyear= jsonObjec.optString("year", "");
                        Double z4 = Double.parseDouble(myyear);
                        String x4 = decimalFormat1.format(z4);
                        BigDecimal bg4=new BigDecimal(x4);
                       year.setText(bg4+"");
                       String  mytotal= jsonObjec.optString("total", "");
                       Double z = Double.parseDouble(mytotal);
                       DecimalFormat decimalFormat = new DecimalFormat("0.00");
                       String x = decimalFormat.format(z);
                       if(x.length()>8){
                           total.setTextSize(17);
                       }
                       BigDecimal bg=new BigDecimal(x);
                       total.setText(bg+"");
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }

                Log.e(TAG, "回调成功" + result);
                if (msCode == 200) {
                }
                if (msCode == 201) {


                }
                if (msCode == 500) {
                    Toast.makeText(getBaseContext().getApplicationContext(), "系统异常", Toast.LENGTH_SHORT).show();
                    Log.e(TAG, "系统异常");
                }else{


                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                Log.e(TAG, "回调失败");
            }
            @Override
            public void onCancelled(CancelledException cex) {

            }
            @Override
            public void onFinished() {
            }
        });


    }

    private void intView2() {
        SharedPreferences sharedPreferences = getSharedPreferences("userinfo.txt", Context.MODE_PRIVATE);
        String userID = sharedPreferences.getString("userID", "");
        RequestParams params = new RequestParams("http://zljl.laiima.com/index.php?s=/lfl_api/Commonlfl/statistics");
        //需要带入的参数
        params.addBodyParameter("uid", userID);
        x.http().post(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                try {
                    JSONObject jsonObject = new JSONObject(result);
                    String data = jsonObject.optString("list", null);
                    if (!TextUtils.isEmpty(data)){
                        JSONObject jsonObjec = new JSONObject(data);
                        String  mytoday= jsonObjec.optString("today", "");
                        uesr_today.setText(mytoday);
                        String  myyesterday= jsonObjec.optString("yesterday", "");
                        user_yesterday.setText(myyesterday);
                        String  mymonth= jsonObjec.optString("month", "");
                        user_month.setText(mymonth);
                        String  myyear= jsonObjec.optString("year", "");
                        useer_year.setText(myyear);
                        String  mytotal= jsonObjec.optString("total", "");
                        user_total.setText(mytotal);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }

                Log.e(TAG, "回调成功" + result);
                if (msCode == 200) {
                }
                if (msCode == 201) {


                }
                if (msCode == 500) {
                    Toast.makeText(getBaseContext().getApplicationContext(), "系统异常", Toast.LENGTH_SHORT).show();
                    Log.e(TAG, "系统异常");
                }else{


                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                Log.e(TAG, "回调失败");
            }
            @Override
            public void onCancelled(CancelledException cex) {

            }
            @Override
            public void onFinished() {
            }
        });


    }
    @Override
    public void onClick(View v) {
        int id=v.getId();
        switch (id){
            case R.id.my_statics_back:
                finish();
                break;
            case R.id.to_my_income:
//                Intent isintent=new Intent(MyStatisticsActivity.this, IncomeStatisticActivity.class);
//                startActivity(isintent);
                break;
            case R.id.to_my_user:
                break;
        }

    }
}
